package com.redwood.report.activity.mvp.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.redwood.report.R;
import com.redwood.report.activity.JMainActivity;
import com.redwood.report.activity.MainActivity;
import com.redwood.report.base.BaseActivity;
import com.redwood.report.common.Config;
import com.redwood.report.entity.bak.been.User;
import com.redwood.report.util.ToastUtils;
import com.redwood.report.util.actionbar.LightStatusBarUtils;
import com.redwood.report.util.actionbar.ToolbarUtil;

/**
 * Created by Supeng on 2018/8/16.
 */


public class LoginActivity extends BaseActivity implements LoginView, View.OnClickListener {
    private Button loginBtn;
    private EditText phoneEt,psdEt,ipEt;;
    private LoginPresenter loginPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //标题栏颜色
        ToolbarUtil.setColorNoTranslucent(this, getResources().getColor(R.color.blueColorAppKey2));
        //深色主题 true字颜色为深色 false 为白色标题字
        LightStatusBarUtils.setLightStatusBar(this, false);
        if(Config.isUserLogin()){
            startActivity(new Intent(getActivity(),JMainActivity.class));
            finish();
            return;
        }
        if(loginPresenter==null){
            loginPresenter=new LoginPresenter(this,this);
        }
        setContentView(R.layout.activity_login);
        initView();

    }

    private void initView() {
        loginBtn=findViewById(R.id.login_btn);
        phoneEt=findViewById(R.id.phone_et);
        psdEt=findViewById(R.id.psd_et);
        loginBtn.setOnClickListener(this);
        ipEt=findViewById(R.id.ip_et);
        ipEt.setText(Config.getIpAdress());
    }

    @Override
    public void loginSuccess() {
        finish();
        startActivity(new Intent(getActivity(), MainActivity.class));
    }

    @Override
    public void loginFail() {
        Toast.makeText( getActivity(), "登陆失败", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void tips(String tips) {
        Toast.makeText( getActivity(), tips, Toast.LENGTH_SHORT).show();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(loginPresenter!=null) {
            loginPresenter.onDestroy();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.login_btn:
              //  loginPresenter.login(phoneEt.getText().toString(),psdEt.getText().toString());
             String ipAdress=ipEt.getText().toString();
                if(TextUtils.isEmpty(ipAdress)){
                    ToastUtils.show("IP地址不能为空");
                    return;
                }
                Config.setIpAdress(ipAdress);
                User user=new User();
                user.setUsername("张三");
                user.setToken("xxxxxxxxxxx");
                Config.setUserInfo(user);
                startActivity(new Intent(getActivity(),JMainActivity.class));
//                int id=radioGroup.getCheckedRadioButtonId();
//           String type="0";
//           switch (id){
//               case R.id.rb_b:
//                   type="0";
//                   break;
//               case R.id.rb_w:
//                   type="1";
//                   break;
//               case R.id.rb_g:
//                   type="2";
//                   break;
//           }
//                User user=new User();
//                user.setType(type);
//                user.setToken("tooken");
//                user.setUserid("1");
//                user.setUsername("哈哈");
//                Config.setUserInfo(user);
//                startActivity(new Intent(getActivity(),MainActivity.class));
//                finish();
//                break;
        }
    }
}
